package com.jt.tenant.activity;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.jt.tenant.R;
import com.jt.tenant.adapter.InsSelectAdapter;
import com.jt.tenant.app.JTApplication;
import com.jt.tenant.conn.GetInsList;
import com.jt.tenant.conn.SetInsOrder;
//import com.jt.tenant.conn.SetNewInsOrder;
import com.jt.tenant.dialog.ResultDialog;
//import com.jt.tenant.dialog.TipInsInfoDialog;
import com.jt.tenant.dialog.TipDialog;
import com.jt.tenant.widget.Tools;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.util.UtilToast;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by AD on 2016/5/12.
 */
public class InsSelectActivity extends JTActivity implements OnClickListener, Serializable {
    private TextView mTitleName, tv_next,right_button;

    private ListView ins_list;

    private LinearLayout lin_ins_call;

    private static List<GetInsList.INS_ITEM> insList = new ArrayList<>();

    private InsSelectAdapter mAdapter;

    private GetInsList getVCIList;

    private int flag;

    private static int buy_year;

    public static List<VCI_SELECTED> selectedList = new ArrayList<>();

    private SetInsOrder setInsOrder;

    private Bundle bundle;

    private static Context mContext;

    public static CommitInfo commitInfo;

    private static String csxID;//车损险ID

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_ins_select);

        Intent intent = getIntent();

        bundle = intent.getExtras();

        initView();

        mContext = InsSelectActivity.this;
    }

    private void initView() {

        setInsOrder = new SetInsOrder(new AsyCallBack<SetInsOrder.Info>() {

            @Override
            public void onSuccess(String toast, int type, SetInsOrder.Info info) throws Exception {

                super.onSuccess(toast, type, info);

                if (info.msg.equals("000")) {

                    JTApplication.jtPreferences.saveInsFlag("1");

                    TipDialog dialog = new TipDialog(context);

                    dialog.setTip("订单提交成功，稍后将为您提供保险报价！");

                    JTApplication.jtPreferences.saveInsFlag("1");

                    dialog.setType(4);

                    dialog.setCanceledOnTouchOutside(false);

                    dialog.show();

                } else {

                    UtilToast.show(context, info.msg);

                }

            }
        });

        (mTitleName = (TextView) findViewById(R.id.tv_title_name)).setText("险种选择");

        (tv_next = (TextView)findViewById(R.id.tv_ins_select_next)).setOnClickListener(this);

        (lin_ins_call = (LinearLayout)findViewById(R.id.lin_ins_call)).setOnClickListener(this);

        ins_list = (ListView) findViewById(R.id.lv_ins_select_list);

        mAdapter = new InsSelectAdapter(this, insList);

        ins_list.setAdapter(mAdapter);



        getVCIList = new GetInsList(new AsyCallBack<GetInsList.Info>() {

            @Override

            public void onSuccess(String toast, int type, GetInsList.Info info) throws Exception {

                super.onSuccess(toast, type, info);

                insList.clear();

                insList.addAll(info.ins_list);

                for(int i = 0 ;i<insList.size();i++){

                    if(insList.get(i).NAME.equals("机动车损失险")){

                        csxID = insList.get(i).ID;

                    }

                }

                insList.clear();

                insList.addAll(info.ins_list);

                mAdapter.notifyDataSetChanged();
//
            }

            @Override
            public void onFail(String toast, int type) throws Exception {
                super.onFail(toast, type);
                UtilToast.show(InsSelectActivity.this, toast);
                finish();
            }

            @Override
            public void onEnd(String toast, int type) throws Exception {
                super.onEnd(toast, type);
                if (!toast.equals("")) {

                    UtilToast.show(InsSelectActivity.this, toast);

                }
            }
        });
        getVCIList.execute(InsSelectActivity.this);

        commitInfo = new CommitInfo() {
            @Override
            public void commit() {

                setInsOrder.companyId = bundle.getString("id");

                setInsOrder.OwnerName = bundle.getString("name");

                setInsOrder.phone = JTApplication.jtPreferences.readPhone();

                setInsOrder.IdCard = bundle.getString("id_number");

                setInsOrder.Platenumber = bundle.getString("plate_number");

                setInsOrder.Vin = bundle.getString("vin");

                setInsOrder.engine = bundle.getString("engine_number");

                setInsOrder.xh = bundle.getString("category_name");

                setInsOrder.yearLimit = bundle.getString("gmnf");

                setInsOrder.sx = "1";

                setInsOrder.typeList = getNameId();

                setInsOrder.numList = getItemId();

                setInsOrder.flagList = getIopId();

                setInsOrder.flag = "0";

                setInsOrder.execute(InsSelectActivity.this);

            }
        };
    }

    public static class VCI_SELECTED {

        public String NAME;

        public String M_ID;

        public String MONEY;//实际传的是数量

        public String IOP;//方法中给2代表不改变现有选项

    }

    public static void putSelect(String name, String m_id, String money, int iop) {

        int a = 0;//a=0 新建 1：保存

        if(isExit(name)) {

            for (int i = 0; i < selectedList.size(); i++) {

                if (selectedList.get(i).NAME.equals(name)) {

                    if (!m_id.equals("0")) {

                        selectedList.get(i).M_ID = m_id;

                    }

                    if (!money.equals("0")) {

                        selectedList.get(i).MONEY = money;
                    }

                    if (iop != 2) {

                        selectedList.get(i).IOP = String.valueOf(iop);

                    }


                }
            }

        }else {

            VCI_SELECTED new_selected = new VCI_SELECTED();

            new_selected.NAME = name;

            new_selected.M_ID = m_id;

            new_selected.MONEY = money;

            if (iop != 2) {

                new_selected.IOP = String.valueOf(iop);

            } else {

                new_selected.IOP = "0";

            }

            selectedList.add(new_selected);

        }
    }

    public static void delSelected(String name) {

        for (int i = 0; i < selectedList.size(); i++) {

            if (selectedList.get(i).NAME.equals(name)) {

                selectedList.remove(i);

            }

        }

    }

    public static boolean isExit(String ID) {

        boolean exist = false;

        for (int i = 0; i < selectedList.size(); i++) {

            if (selectedList.get(i).NAME.equals(ID)) {

                exist = true;

            }

        }

//        Log.e("ISEXIT=======",String.valueOf(exist));

        return exist;

    }

    public static VCI_SELECTED getItem(String ID) {

        VCI_SELECTED mItem = new VCI_SELECTED();

        for (int i = 0; i < selectedList.size(); i++) {

            if (selectedList.get(i).NAME.equals(ID)) {

                mItem = selectedList.get(i);

            }

        }

        return mItem;

    }


    public static List<VCI_SELECTED> getSelected() {

        return selectedList;

    }

    private String getNameId() {

        String s = "";

        for (int i = 0; i < selectedList.size(); i++) {

            s += Tools.F_num(selectedList.get(i).M_ID) + ",";

        }

//        Log.e("ID====",s);

        return s;

    }

    private String getItemId() {

        String s = "";

        for (int i = 0; i < selectedList.size(); i++) {

            s += selectedList.get(i).MONEY + ",";

        }

//        Log.e("ITEMID====",s);

        return s;


    }

    private String getIopId() {

        String s = "";

        for (int i = 0; i < selectedList.size(); i++) {

            s += selectedList.get(i).IOP + ",";

        }

//        Log.e("IOPID====",s);

        return s;

    }

     public abstract class CommitInfo {

        public abstract void commit();

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        commitInfo = null;

    }

    @Override
    protected void onResume() {

        super.onResume();

        selectedList.clear();

    }

    @Override
    public void onClick(View view) {

        super.onClick(view);

        switch (view.getId()){

            case R.id.tv_ins_select_next:

                if(selectedList.size() == 0){

                    UtilToast.show(this,"请至少选择一种保险购买");

                }else {

                    TipDialog tipInsInfoDialog = new TipDialog(InsSelectActivity.this);

                    tipInsInfoDialog.setTip("是否确认查询报价？如车辆信息有误将无法报价");

                    tipInsInfoDialog.setType(3);

                    tipInsInfoDialog.show();

                }

                break;

            case R.id.lin_ins_call:

                Tools.Call(this,"4006115616");

                break;




        }
    }
}
